package com.dinghui.salary.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dinghui.salary.enums.ResponseStatusEnum;
import com.dinghui.salary.exception.CommonException;
import com.dinghui.salary.mapper.MonthSalaryMapper;
import com.dinghui.salary.pojo.MonthSalary;
import com.dinghui.salary.pojo.SalaryType;
import com.dinghui.salary.pojo.dto.MonthSalaryDTO;
import com.dinghui.salary.pojo.vo.MonthSalaryVO;
import com.dinghui.salary.service.MonthSalaryService;
import com.dinghui.salary.utils.GetUUID;
import com.dinghui.salary.utils.PagedGridResult;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author tanrentao
 * @date 2021-09-09
 * @desc
 */
@Service(version = "2.0.0")
public class MonthSalaryServiceImpl extends BaseService implements MonthSalaryService {

    @Autowired
    private MonthSalaryMapper monthSalaryMapper;

    @Override
    public int create(MonthSalaryDTO dto) {
        Example example = new Example(MonthSalary.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("monthTypeId", dto.getMonthTypeId());
        criteria.andEqualTo("salaryTypeId", dto.getSalaryTypeId());
        MonthSalary isMonthSalary = monthSalaryMapper.selectOneByExample(example);
        if (ObjectUtils.isNotEmpty(isMonthSalary)) {
            CommonException.display(ResponseStatusEnum.DATA_EXIST_ERROR);
        }

        MonthSalary monthSalary = new MonthSalary();
        BeanUtils.copyProperties(dto, monthSalary);
        monthSalary.setId(GetUUID.getUUID());
        int result = monthSalaryMapper.insertSelective(monthSalary);
        return result;
    }

    @Override
    public int update(MonthSalaryDTO dto) {
        Example example = new Example(MonthSalary.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("monthTypeId", dto.getMonthTypeId());
        criteria.andEqualTo("salaryTypeId", dto.getSalaryTypeId());
        MonthSalary isMonthSalary = monthSalaryMapper.selectOneByExample(example);
        if (ObjectUtils.isNotEmpty(isMonthSalary)) {
            CommonException.display(ResponseStatusEnum.DATA_EXIST_ERROR);
        }

        MonthSalary monthSalary = new MonthSalary();
        BeanUtils.copyProperties(dto, monthSalary);

        Example selectExample = new Example(MonthSalary.class);
        selectExample.createCriteria().andEqualTo("id", dto.getId());

        int result = monthSalaryMapper.updateByExampleSelective(monthSalary, selectExample);
        return result;
    }

    @Override
    public int delete(String id) {
        Example example = new Example(SalaryType.class);
        example.createCriteria().andEqualTo("id",id);
        int result = monthSalaryMapper.deleteByExample(example);
        return result;
    }

    @Override
    public PagedGridResult selectMonthSalaryList(MonthSalaryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<MonthSalaryVO> list = monthSalaryMapper.selectMonthSalaryList(dto.getMonthTypeId());
        return setterPagedGrid(list, dto.getPage());
    }
}
